﻿Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Threading
Imports System.ComponentModel
Imports System.Windows.Threading
Imports System.IO

''' <summary>
''' Interaction logic for ScreenSaverWindow.xaml
''' </summary>
Partial Public Class ScreenSaverWindow
    Inherits Window

    Private userInfo As New UserInfo()
    Private r As New Random()
    Private WithEvents countdownClock As DispatcherTimer
    Private files As String()
    Dim myPicturesPath As String
    Private WithEvents zoomStoryBoard As Storyboard
    Private conv As New EventCountdownConverter()
    Private settingsEvent As New EventWaitHandle(False, EventResetMode.AutoReset, "C4F-WhileAway-Settings")

    Public Sub New()
        InitializeComponent()

        userInfo.Load()
        Me.DataContext = userInfo
    End Sub

    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.Loaded

        zoomStoryBoard = DirectCast(Me.Resources("PhotoEffectStoryboard"), Storyboard)

        ' Every second update clock if set
        countdownClock = New DispatcherTimer(New TimeSpan(0, 0, 1), DispatcherPriority.Background, New EventHandler(AddressOf countdownClock_Tick), Me.Dispatcher)
        countdownClock.Start()

        ' Every ten seconds rotate image if enabled
        If userInfo.PhotosEnabled Then
            myPicturesPath = userInfo.PhotoLocation

            ThreadPool.QueueUserWorkItem(AddressOf DoWorkItem)
        End If
    End Sub

    Private Sub DoWorkItem()
        files = Directory.GetFiles(myPicturesPath, "*.jpg", SearchOption.TopDirectoryOnly)
        Dispatcher.BeginInvoke(New Action(AddressOf SetBackgroundImageRandom))
    End Sub

    Private Sub PhotoEffectStoryboardCompleted(ByVal sender As Object, ByVal e As EventArgs) Handles zoomStoryBoard.Completed
        SetBackgroundImageRandom()
    End Sub

    Sub countdownClock_Tick(ByVal sender As Object, ByVal e As EventArgs)
        userInfo.UpdateReturnTimeRemaining()
    End Sub

    Private Sub SetBackgroundImageRandom()
        If files Is Nothing OrElse files.Length = 0 Then
            Return
        End If

        Dim idx As Integer = r.[Next](files.Length)
        Dim filename As String = files(idx)

        SetBackgroundImage(filename)
    End Sub

    Private Sub SetBackgroundImage(ByVal filename As String)
        Dim handler As New BitmapImage(New Uri(filename))
        AddHandler handler.DownloadFailed, New EventHandler(Of ExceptionEventArgs)(AddressOf ScreenSaverWindow_DownloadFailed)

        MyImage.Source = handler

        zoomStoryBoard.Begin(MyImage)
    End Sub

    Sub ScreenSaverWindow_DownloadFailed(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        ' We'll move on then!
        SetBackgroundImageRandom()
    End Sub

    Private Sub buttonImBack_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles buttonImBack.Click
        ' Reset time away for next run
        userInfo.ReturnTime = DateTime.MinValue
        userInfo.Save()

        settingsEvent.Set()

        Application.Current.Shutdown()
    End Sub
End Class
